package com.example.list;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarLayout;
import com.haibin.calendarview.CalendarView;

public class CalendarPopWindow extends PopupWindow implements View.OnClickListener, CalendarView.OnMonthChangeListener, CalendarView.OnCalendarSelectListener {

    private Context context;
    View mPopView;

    private TextView tvClose;
    private TextView tvConfirm;
    private TextView tvTitle;
    private ImageView imgYearPre;
    private ImageView imgMonthPre;
    private TextView tvMonth;
    private ImageView imgMonthNext;
    private ImageView imgYearNext;
    private CalendarLayout calendarLayout;
    private CalendarView calendarView;
    int currentYear;
    String title = "";

    IcalendarClick icalendar;

    /**
     * 构造方法：动态初始化BottomPopWinDow
     *
     * @param context
     */
    public CalendarPopWindow(Context context) {
        super(context);
        init(context);
    }


    public CalendarPopWindow(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CalendarPopWindow(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        mPopView = LayoutInflater.from(context).inflate(R.layout.layout_popup_calendar, null);
        setContentView(mPopView);
        setWidth(LinearLayout.LayoutParams.MATCH_PARENT);// 设置弹出窗口的宽
        setHeight(LinearLayout.LayoutParams.MATCH_PARENT);// 设置弹出窗口的高
        // 点击popuwindow外让其消失
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable(0x30000000));
        tvClose = (TextView) mPopView.findViewById(R.id.tv_close);
        tvConfirm = (TextView) mPopView.findViewById(R.id.tv_confirm);
        tvTitle = (TextView) mPopView.findViewById(R.id.tv_title);

        tvMonth = (TextView) mPopView.findViewById(R.id.tv_month);

        calendarLayout = (CalendarLayout) mPopView.findViewById(R.id.calendarLayout);
        calendarView = (CalendarView) mPopView.findViewById(R.id.calendarView);


        imgYearPre = (ImageView) mPopView.findViewById(R.id.img_yearPre);
        imgMonthPre = (ImageView) mPopView.findViewById(R.id.img_monthPre);

        imgMonthNext = (ImageView) mPopView.findViewById(R.id.img_monthNext);
        imgYearNext = (ImageView) mPopView.findViewById(R.id.img_yearNext);
        currentYear = calendarView.getCurYear();

        tvMonth.setText(calendarView.getCurYear() + "年" + calendarView.getCurMonth() + "月");
        tvClose.setOnClickListener(this);
        tvConfirm.setOnClickListener(this);
        imgMonthPre.setOnClickListener(this);
        imgYearPre.setOnClickListener(this);
        imgMonthNext.setOnClickListener(this);
        imgYearNext.setOnClickListener(this);

        calendarView.setOnMonthChangeListener(this);//监听 滑动日历，月份改变

        calendarView.setOnCalendarSelectListener(this);


    }

    public void setOnCalendarClick(IcalendarClick context) {
        icalendar = context;
    }

    public void setTitle(String title) {
        this.title = title;
        tvTitle.setText(title);
    }

    public void setDate() {
        calendarView.scrollToCalendar(2021,6,26);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_close:
                if (mPopView != null) {
                    dismiss();
                }
                break;
            case R.id.tv_confirm:
                if (mPopView != null) {
                    dismiss();
                }
                break;

            case R.id.img_yearPre:
                currentYear = currentYear - 1;
//                calendarView.scrollToYear(currentYear);  //scrollToYear 调用失败，无效，改用scrollToCalendar
                calendarView.scrollToCalendar(currentYear, 1, 1);
                break;

            case R.id.img_monthPre:
                calendarView.scrollToPre();
                break;
            case R.id.img_monthNext:
                calendarView.scrollToNext();
                break;

            case R.id.img_yearNext:
                currentYear = currentYear + 1;
//                calendarView.scrollToYear(currentYear);  //scrollToYear 调用失败，无效，改用scrollToCalendar
                calendarView.scrollToCalendar(currentYear, 1, 1);
                break;
        }

    }

    public void show() {
        showAtLocation(((Activity) context).getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
    }


    @Override
    public void onMonthChange(int year, int month) {
        tvMonth.setText(year + "年" + month + "月");
    }

    @Override
    public void onCalendarOutOfRange(Calendar calendar) {

    }

    @Override
    public void onCalendarSelect(Calendar calendar, boolean isClick) {

        String sMonth = addZero(calendar.getMonth());
        String sDay = addZero(calendar.getDay());

        String date = calendar.getYear() + sMonth + sDay;

        if (icalendar != null) {
            icalendar.dateChoosed(date, title);
        }

    }

    private String addZero(int num) {
        if (num > 0 && num < 10) {
            return "-0" + num;
        } else {
            return "-" + num;
        }
    }

    public interface IcalendarClick {
        void dateChoosed(String date, String title);
    }

}
